package com.cratos.xxgram.platf.message;

import javax.persistence.*;
import org.redkale.convert.json.*;
import com.cratos.xxgram.platf.base.BaseEntity;
import java.io.Serializable;
import org.redkale.convert.ConvertColumn;
import org.redkale.convert.ConvertType;
import org.redkale.source.DistributeTable;
import org.redkale.source.DistributeTableStrategy;
import org.redkale.source.FilterNode;
import org.redkale.source.Range;
import org.redkale.util.*;
/**
 *
 * @author SquRab
 */
@Table(comment = "聊天消息记录")
@DistributeTable(strategy = MessageRecord.TableStrategy.class)
public class MessageRecord extends BaseEntity {

    private static final Reproduce<MessageRecord, MessageRecord> reproduce = Reproduce.create(MessageRecord.class, MessageRecord.class);
    
    private static final Reproduce<OfflineMsgRecord, MessageRecord> offLineReproduce = Reproduce.create(OfflineMsgRecord.class, MessageRecord.class);
    
    @Comment("历史消息存储 便于oss 查询")
    private static final Reproduce<MessageRecordHis, MessageRecord> hisReproduce = Reproduce.create(MessageRecordHis.class, MessageRecord.class);
    
    public static final short UNREAD_STATUS = 10;//回执状态 未读  
    
    public static final short READ_STATUS = 20; // 已读
    
    public static final short CANCLE_STATUS = 60; //撤销发送
      
    @Id
    @Column(length = 64, comment = "fromid+'-'+targetid+'-'+create36time")
    private String messageid = "";

    @Comment("当前操作用户名称")
    @Transient
    private String operatorUserName;
    
    @Comment("当前操作用户Id")
    @Transient
    private int operatorUserId;
    
    @Column(comment = "发送方ID  用户ID")
    private int fromid;

    @Column(comment = "接收方ID  用户ID  群ID")
    private int targetid;

    @Comment("系统消息")
    public  static final  short MSG_KIND_SYSTEM = 10;
    
    @Comment("单聊")
    public static final  short MSG_KIND_PRIVATE = 20;
    
    @Comment("群聊")
    public  static final  short MSG_KIND_GROUP = 30;
    
    @Comment("聊天室")
    public  static final  short MSG_KIND_CHATROOM = 40;
    
    @Column(comment = "消息分类 10 系统消息  20 单聊  30 群组消息 40 聊天室消息")
    private short msgkind;

    @Column(comment = "回执状态  10 未读  20 已读  60 撤回")
    private short status = 10;

    @Column(comment = "消息类型")
    private  short msgtype;

    @Column(length = 2048, updatable = false, comment = "消息内容")
    private String content;
    
    @Column(updatable = false, comment = "发送时间")
    private long createtime;

    @Column(comment = "读消息时间")
    private long readtime;

    @Transient
    private BaseContent contentObj;
   
    @Comment("消息加密禁用")
    public static final short ENCRYPT_DISABLE = 10;
    
    @Comment("消息加密启用")
    public static final short ENCRYPT_ENABLE = 20;
    
    @Transient
    private short encrypt = ENCRYPT_DISABLE; // 10 消息未加密 20 消息经过加密

    @Transient
    private String clienttype = "OSS";
    
    @Comment("客户端语言类型")
    @Transient
    private String local;
    
    
    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getLocal() {
		return local;
	}

	public void setLocal(String local) {
		this.local = local;
	}

	public short getEncrypt() {
        return encrypt;
    }

    public void setEncrypt(short encrypt) {
        this.encrypt = encrypt;
    }
    
	public String getClienttype() {
		return clienttype;
	}

	public void setClienttype(String clienttype) {
		this.clienttype = clienttype;
	}

	public MessageRecord copy(String content) {
        MessageRecord messageRecord = reproduce.apply(new MessageRecord(), this);
        messageRecord.setContent(content);
        return messageRecord;
    }
    public MessageRecord copy() {
        MessageRecord messageRecord = reproduce.apply(new MessageRecord(), this);
        return messageRecord;
    }
    @Comment("离线消息处理")
    public OfflineMsgRecord copy(int receiveid) {
        OfflineMsgRecord messageRecord = offLineReproduce.apply(new OfflineMsgRecord(), this);
        messageRecord.setReceiverid(receiveid);
        messageRecord.setOfflinerecordid(messageRecord.getMessageid() + "-" + receiveid);// 接受用户唯一id
        return messageRecord;
    }
    
     @Comment("oss查询消息处理")
    public MessageRecordHis copyhis() {
        MessageRecordHis messageRecord = hisReproduce.apply(new MessageRecordHis(), this);
        return messageRecord;
    }
    
    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public String getOperatorUserName() {
        return operatorUserName;
    }

    public void setOperatorUserName(String operatorUserName) {
        this.operatorUserName = operatorUserName;
    }

    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public int getOperatorUserId() {
        return operatorUserId;
    }

    public void setOperatorUserId(int operatorUserId) {
        this.operatorUserId = operatorUserId;
    }
    
    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public BaseContent getContentObj() {
        return contentObj;
    }

    public void setContentObj(BaseContent contentObj) {
        this.contentObj = contentObj;
    }
    
    public void setMessageid(String messageid) {
        this.messageid = messageid;
    }
    
 

    public String getMessageid() {
        return this.messageid;
    }

    public void setFromid(int fromid) {
        this.fromid = fromid;
    }

    public int getFromid() {
        return this.fromid;
    }

    public void setTargetid(int targetid) {
        this.targetid = targetid;
    }

    public int getTargetid() {
        return this.targetid;
    }

    public void setMsgkind(short msgkind) {
        this.msgkind = msgkind;
    }

    public short getMsgkind() {
        return this.msgkind;
    }

    public void setStatus(short status) {
        this.status = status;
    }

    public short getStatus() {
        return this.status;
    }

    public short getMsgtype() {
        return msgtype;
    }

    public void setMsgtype(short msgtype) {
        this.msgtype = msgtype;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setCreatetime(long createtime) {
        this.createtime = createtime;
    }
    
    public long getCreatetime() {
        return this.createtime;
    }

    public void setReadtime(long readtime) {
        this.readtime = readtime;
    }

    @ConvertColumn(ignore = true, type = ConvertType.JSON)
    public long getReadtime() {
        return this.readtime;
    }

    @Override
    public String toString() {
        return JsonConvert.root().convertTo(this);
    }
    
    @Comment("生成策略")
    public static class TableStrategy implements DistributeTableStrategy<MessageRecord> {
        
        public static final String format = "%1$tY%1$tm%1$td";

        @Override
        public String getTable(String table, MessageRecord bean) {
            return getTable(table, (Serializable) bean.getMessageid());
        }

        @Override
        public String getTable(String table, Serializable primary) {
            String id = (String) primary;
            System.out.println(id.substring(0, id.indexOf('-')));
            String time = id.split("-")[2];
            return getSingleTable(table, Long.parseLong(time, 36));
        }

        @Override
        public String getTable(String table, FilterNode node) {
            Object time = node.findValue("createtime");
            if (time instanceof Long) return getSingleTable(table, (Long) time);
            Range.LongRange createtime = (Range.LongRange) time;
            return getSingleTable(table, createtime.getMin());
        }

        private String getSingleTable(String table, long createtime) {
            int pos = table.indexOf('.');
            return table.substring(pos + 1) + "_" + String.format(format, createtime);
        }
    }
}
